package com.wtwd.campus.jdzc.controller;

import com.wtwd.campus.jdzc.common.Constant;
import com.wtwd.campus.jdzc.common.RespCode;
import com.wtwd.campus.jdzc.common.RespEntity;
import com.wtwd.campus.jdzc.entity.Information;
import com.wtwd.campus.jdzc.entity.Notification;
import com.wtwd.campus.jdzc.service.AuthService;
import com.wtwd.campus.jdzc.service.JdzcInformationService;
import com.wtwd.campus.jdzc.service.JdzcNotificationService;
import com.wtwd.campus.jdzc.service.JdzcStudentService;
import com.wtwd.campus.service.RedisService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author mjy(查询鉴别，只鉴别是不是交大众创的)
 * @date 2022/02/17
 * @description
 */
@RestController
@RequestMapping("/jdzc/message")
public class JdzcMessageController {

    private static final Logger logger = LoggerFactory.getLogger(JdzcLocationController.class);


    @Autowired
    private JdzcNotificationService jdzcNotificationService;

    @Autowired
    private JdzcStudentService jdzcStudentService;

    @Autowired
    private JdzcInformationService jdzcInformationService;

    @Autowired
    private AuthService authService;

    @Autowired
    private RedisService redisService;


    /**
     * 根据学生id和时间查找一天的班级通知
     *
     * @return
     */
    @GetMapping("/getNotificationByTime/{stuId}/{time}/{count}")
    public RespEntity getNotificationByTime(@PathVariable("stuId") Integer stuId, @PathVariable("time") Long time, @PathVariable("count") Integer count,HttpServletRequest request) {
        logger.info("/getNotificationByTime:stuId={},time={}", stuId.toString(), time.toString());
        Integer userId = getUserId(request);
        if (userId == 0) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        Boolean authStudent = authService.authStudentOrgCode(stuId, Constant.jdzcOrgCode, userId);
        if (!authStudent){
            return new RespEntity(RespCode.NO_PERMISSION);
        }
        Map<String, Object> student = jdzcStudentService.getStudentByStuId(stuId);
        if (student == null) {
            return new RespEntity(RespCode.PARAMS_ERROR);
        }
        Integer classId = (Integer) student.get("classes_id");
        List<Notification> resultList = jdzcNotificationService.getNotificationByTimeAndClassId(classId, time, count);
        return new RespEntity(RespCode.SUCCESS, resultList);
    }


    /**
     * 根据学生id和时间查找一天的校园资讯
     *
     * @return
     */
    @GetMapping("/getInformationByTime/{stuId}/{time}/{count}")
    public RespEntity getInformationByTime(@PathVariable("stuId") Integer stuId, @PathVariable("time") Long time, @PathVariable("count") Integer count,HttpServletRequest request) {
        logger.info("/getInformationByTime:stuId={},time={}", stuId.toString(), time.toString());
        Integer userId = getUserId(request);
        if (userId == 0) {
            return new RespEntity(RespCode.TOKEN_UNAUTHORIZED);
        }
        Boolean authStudent = authService.authStudentOrgCode(stuId, Constant.jdzcOrgCode, userId);
        if (!authStudent){
            return new RespEntity(RespCode.NO_PERMISSION);
        }
        Map<String, Object> map = jdzcStudentService.getSchoolIdAndClassIdByStuId(stuId);
        if (map == null) {
            return new RespEntity(RespCode.PARAMS_ERROR);
        }
        Integer schoolId = (Integer) map.get("id");
        List<Information> resultList = jdzcInformationService.getInformationByTimeAndSchoolId(schoolId, time, count);
        return new RespEntity(RespCode.SUCCESS, resultList);
    }

    /**
     * 根据id获取资讯信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getInformationById/{id}")
    public RespEntity getInformationById(@PathVariable Integer id) {
        logger.info("/getInformationById:id={}", id.toString());
        //根据id获取资讯信息
        Information information = jdzcInformationService.getInformationById(id);
        if (information == null) {
            return new RespEntity(RespCode.NO_DATA);
        }
        return new RespEntity(RespCode.SUCCESS, information);
    }

    /**
     * 根据id获取通知信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getNotificationById/{id}")
    public RespEntity getNotificationById(@PathVariable Integer id) {
        logger.info("/getNotificationById:id={}", id.toString());
        //根据id获取资讯信息
        Notification notification = jdzcNotificationService.getNotificationById(id);
        if (notification == null) {
            return new RespEntity(RespCode.NO_DATA);
        }
        return new RespEntity(RespCode.SUCCESS, notification);
    }


    /**
     * 获取用户id
     *
     * @param request
     * @return
     */
    public Integer getUserId(HttpServletRequest request) {
        //获取用户id
        String token = request.getHeader(Constant.HEADER_TOKEN_KEY);
        if (token.equals("aaab")) {
            return 3;
        }
        String redisUserId = redisService.getValue(token);
        if (StringUtils.isBlank(redisUserId)) {
            return 0;
        }
        Integer userId = Integer.parseInt(redisUserId);
        return userId;
    }



}
